package com.ck.chuangke.controller;

import com.ck.chuangke.service.CkDeviceInfoService;
import com.ck.core.BaseController;
import com.ck.export.util.SimpleExportUtil;
import com.ck.model.chuangke.vo.CkDeviceDetailVO;
import com.ck.model.chuangke.vo.CkDeviceInfoListExportVO;
import com.ck.model.chuangke.vo.CkDeviceInfoListVO;
import com.ck.model.chuangke.vo.CkDeviceLogListVO;
import com.ck.model.common.page.PageInfo;
import com.ck.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.List;

/**
 * 
 * <p>Title: CkDeviceInfoController</p>  
 * <p>Description: 创客平台管理终端 ctrl</p>  
 * @author huangf 
 * @date 2018年6月21日
 */
@Controller
@RequestMapping(value = "/ckDeviceInfo")
public class CkDeviceInfoController extends BaseController{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired 
	private CkDeviceInfoService ckDeviceInfoService;


    @RequestMapping(value = "/listByPage")
    public Object listByPage(String deviceName,String companyName,String startTime,String endTime, String deviceCode, String deviceMac,
    		String province, String city, String area, Integer pageNum, Integer pageSize, Model model) {
    	Long start = null;
    	Long end = null;
		try {
			if ( StringUtils.isNotBlank(startTime) ) {
				start = DateUtils.stringTimeToLongTimeStamp(startTime, DateUtils.PATTEN_YMD_HMS);
			}
			if ( StringUtils.isNotBlank(endTime) ) {
				end = DateUtils.stringTimeToLongTimeStamp(endTime, DateUtils.PATTEN_YMD_HMS);
			}
		} catch (ParseException e) {
			logger.error("时间转换异常："+e.getMessage(),e);
			e.printStackTrace();
		}
    	PageInfo<CkDeviceInfoListVO> pageInfo = ckDeviceInfoService.listByPage(start, end, deviceCode, deviceMac,
				companyName,deviceName,province,city,area, pageNum, pageSize);
    	model.addAttribute("list", pageInfo.getList());
		model.addAttribute("total", pageInfo.getTotal());
		model.addAttribute("pageNum", pageInfo.getPageNum());
		model.addAttribute("pageSize", pageInfo.getPageSize());
		model.addAttribute("startTime", startTime);
		model.addAttribute("endTime", endTime);
		model.addAttribute("deviceCode", deviceCode);
		model.addAttribute("deviceMac", deviceMac);
		model.addAttribute("companyName", companyName);
		model.addAttribute("deviceName", deviceName);
		model.addAttribute("province", province);
		model.addAttribute("city", city);
		model.addAttribute("area", area);
    	return new ModelAndView("ck/ck_deviceInfo_list");
	}
    
    /**
     * 导出整体终端报表
     * @param companyName  商家名称
     * @param startTime  时间范围
     * @param endTime  时间范围
     * @param deviceCode 终端机器码
     * @param deviceMac  终端mac地址
     * @param province
     * @param city
     * @param area
     * @param response
     */
    @RequestMapping(value = "/exportDeviceReport")
    public void exportDeviceReport(String companyName,String deviceName,String startTime,String endTime, String deviceCode, String deviceMac,
    		String province, String city, String area, HttpServletResponse response) {
        try (OutputStream out = response.getOutputStream()){
			Long start = null;
			Long end = null;
			try {
				if ( StringUtils.isNotBlank(startTime) ) {
					start = DateUtils.stringTimeToLongTimeStamp(startTime, DateUtils.PATTEN_YMD_HMS);
				}
				if ( StringUtils.isNotBlank(endTime) ) {
					end = DateUtils.stringTimeToLongTimeStamp(endTime, DateUtils.PATTEN_YMD_HMS);
				}
			} catch (ParseException e) {
				logger.error("时间转换异常："+e.getMessage(),e);
				e.printStackTrace();
			}
            List<CkDeviceInfoListExportVO> exportList = ckDeviceInfoService.exportDeviceReport(start, end, deviceCode, deviceMac,
    				companyName,deviceName,province,city,area);
            // 设置输出类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            // 设置文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("终端报表.xls").getBytes(), "ISO-8859-1"));
            SimpleExportUtil.exportToExcel(exportList, "终端报表").write(out);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CkDeviceInfoController exportDeviceReport 导出终端报表失败：" + e.getMessage());
        }
    }
    
    /**
     * 终端日志
     * @param deviceCode
     * @param dateRange  时间范围
     * @param logType 日志类型
     * @param pageNum
     * @param pageSize
     * @param model
     */
    @RequestMapping(value = "/deviceLog")
    public Object deviceLog(String deviceCode, String dateRange, String logType,Integer pageNum, Integer pageSize, Model model) {
    	Long start = null;
    	Long end = null;
    	if ( StringUtils.isNotBlank(dateRange) ) {
    		try {
				start = DateUtils.stringTimeToLongTimeStamp(dateRange.split("-")[0], DateUtils.PATTEN_YMD1);
				end = DateUtils.stringTimeToLongTimeStamp(dateRange.split("-")[1], DateUtils.PATTEN_YMD1);
			} catch (ParseException e) {
				logger.error("时间转换异常："+e.getMessage(),e);
				e.printStackTrace();
			}
    	}
    	logType = "ALL".equals(logType) ? null : logType ;
    	PageInfo<CkDeviceLogListVO> pageInfo = ckDeviceInfoService.listDeviceLogs(start, end, deviceCode,logType,pageNum, pageSize);
    	model.addAttribute("list", pageInfo.getList());
		model.addAttribute("total", pageInfo.getTotal());
		model.addAttribute("pageNum", pageInfo.getPageNum());
		model.addAttribute("pageSize", pageInfo.getPageSize());
		model.addAttribute("dateRange", dateRange);
		model.addAttribute("deviceCode", deviceCode);
		model.addAttribute("logType", logType);
    	return new ModelAndView("ck/ck_deviceLog_list");
    }
    
    
    /**
     * 终端详情
     * @param deviceCode code
     * @param model
     */
    @RequestMapping(value = "/deviceDetail")
    public Object detail(String deviceCode,Model model) {
    	CkDeviceDetailVO detail = ckDeviceInfoService.getDeviceDetail(deviceCode);
    	model.addAttribute("device", detail);
    	return new ModelAndView("ck/ck_device_detail");
    }
    
}
